using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using DotNetMatrix;
using System.Threading;

namespace Blimpbots.Recognition
{
    public class JpegCamera : ProjectiveCamera, ICamera
    {
        public int FrameNumber { get { return _curJpeg; } }

        private ImageCodecInfo ici = GetEncoderInfo("image/jpeg");

        private string[] _jpegList;
        private int _curJpeg;

        private int _width, _height;

        public JpegCamera(int width, int height, string[] jpegList, CameraProperties properties)
            : base (properties)
        {
            _width = width; _height = height;

            _curJpeg = 0;
            _jpegList = jpegList;
        }

        #region ICamera Members

        public event EventHandler NewFrame;

        public Bitmap LastFrame
        {
            get {

                Bitmap b = null;

                do
                {
                    if (_curJpeg >= _jpegList.Length) return null;

                    try
                    {
                        b = (Bitmap)Bitmap.FromFile(_jpegList[_curJpeg++]);

                        if (b.Width != _width || b.Height != _height)
                        {
                            Bitmap resized = new Bitmap(_width, _height);
                            using (Graphics g = Graphics.FromImage(resized))
                            {
                                g.InterpolationMode
                                    = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                                g.DrawImage(b, 0, 0, _width, _height);
                            }

                            b.Dispose();

                            //SaveJPGWithCompressionSetting(resized, _jpegList[_curJpeg] + ".r.jpg", (long)85);

                            b = resized;
                        }

                    }
                    catch { /* ignore failures */ }

                } while (b == null);

                return b;
            }
        }

        public DateTime LastFrameTimestamp
        {
            get { return DateTime.Now; }
        }

        public string Name
        {
            get { return "JpegCamera"; }
        }

        public Bitmap CaptureFrame()
        {
            return LastFrame;
        }

        public void Start() { }
        public void Stop() { }

        #endregion



        private static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }

        private void SaveJPGWithCompressionSetting(Image image, string szFileName, long lCompression)
        {
            EncoderParameters eps = new EncoderParameters(1);
            eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, lCompression);
            image.Save(szFileName, ici, eps);
        }



    }
}
